package cn.hpe.services.impl;

import cn.hpe.dao.DataTypeDao;
import cn.hpe.po.Datatype;
import cn.hpe.services.IndexServices;
import com.sun.prism.PixelFormat;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qq601 on 2016/9/30 0030.
 */

@Service
public class IndexServicesImpl implements IndexServices{

    @Autowired
    DataTypeDao dataTypeDao;

    public boolean initIndexPage(){

        List<Datatype> dtList = dataTypeDao.findByProperty("dtparentId",0);

        HttpServletRequest httpRequest = ServletActionContext.getRequest();
        httpRequest.setAttribute("DTList",dtList);
        httpRequest.setAttribute("BigTypeCount",dtList.size());
        List<Map<Datatype,List<Datatype>>> childDtList= new ArrayList<>();
        for(Datatype dt :dtList) {
            List<Datatype> tempList = dataTypeDao.findByProperty("dtparentId",dt.getDtid());
            Map<Datatype,List<Datatype>> map = new HashMap<>();
            for(Datatype d:tempList){
                map.put(d,dataTypeDao.findByProperty("dtparentId",d.getDtid()));
            }
            childDtList.add(map);
        }

        httpRequest.setAttribute("ChildList",childDtList);


        return true;
    }


}
